import pako from 'pako'
function uint8ArrToInt(uint8Arr) {
  var arr = []
  for (var i = 0; i < uint8Arr.length; i++) {
    arr.push(uint8Arr[i])
  }
  return arr
}
export function gzip(json) {
  let text = JSON.stringify(json)
  let gzip = pako.gzip(text, { to: 'string', level: 6 })
  let result = uint8ArrToInt(gzip).toString()
  return result
}

export function ungzip(str) {
  if (!str) return {}
  try {
    let result = JSON.parse(str)
    return result
  } catch (error) {
    let c = []
    str.split(',').forEach((v) => {
      c.push(parseInt(v))
    })
    let gzip = pako.ungzip(new Uint8Array(c), { to: 'string' })
    let result = JSON.parse(gzip)
    return result
  }
}
